package com.ydgk.jishuzhan.controller;


import com.ydgk.jishuzhan.beans.AjaxResult;
import com.ydgk.jishuzhan.beans.Message;
import com.ydgk.jishuzhan.beans.Page;
import com.ydgk.jishuzhan.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private BlogService blogService;


    @Autowired
    private QuestionService questionService;

    @Autowired
    private TalkService talkService;

    @Autowired
    private UserService userService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private MessageService messageService;



    @RequestMapping(value = "/toIndex", method = RequestMethod.GET)
    public String toIndex(@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                          @RequestParam(value = "pageSize", required = false, defaultValue = "2") Integer pageSize,
                          @RequestParam(value= "userId" ,required = false ,defaultValue="1") Integer userId,
                          ModelMap modelMap) {

        Integer pageStart = (pageNo - 1) * pageSize;

        // 将请求参数封装成一个map
        Map<String, Object> map = new HashMap<>();
        map.put("start", pageStart);
        map.put("size", pageSize);
        map.put("userId",userId);

        // 获取用户数据
        List<Message> messages = messageService.getMessages(map); // limit 0 , 2     pageNo      pageStart: (pageNo - 1) * pageSize , pageSize

        // 将获取到的数据存入到模型中
        //modelMap.addAttribute("messages",messages);
        //将页面中需要的数据都传入到模型中   比如  pageNo   pageTotal  messages

        // 页码的总数   ===>  数据的条数    /   页面的大小   == > 页码的总数
        Integer pageTotal = 0;
        // 数据的条数
        Integer MessageTotal = messageService.queryMessageTotal( map );

        if (MessageTotal % pageSize == 0) {
            pageTotal = MessageTotal / pageSize;
        } else {
            pageTotal = MessageTotal / pageSize + 1;
        }

        // 将数据封装到模型中
//        modelMap.addAttribute("messages",messages);
//        modelMap.addAttribute("pageNo",pageNo);
//        modelMap.addAttribute("pageTotal",pageTotal);

        // 构建一个对象，将需要的数据封装到对象中，传入页面时直接传入对象
        Page<Message> page = new Page(messages, pageNo, pageTotal);

        modelMap.addAttribute("page",page);

        return "message";
    }

    @RequestMapping("/toMessage-detail")
    public String toDetail(Integer id, Model model){
        Message message=messageService.findMessage(id);
        model.addAttribute("message",message);
        String sendUser=userService.findUserById(message.getSendId()).getUsername();
        model.addAttribute("sendUser",sendUser);
        messageService.setRead(id);
        return "message-detail";
    }

    @ResponseBody
    @RequestMapping("/batchDelete")
    public AjaxResult batchDelete(@RequestParam("ids") Integer[] ids){
        AjaxResult ajaxResult = new AjaxResult();
        try{
            // 批量删除
           messageService.batchDelete(ids);
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            e.printStackTrace();
        }

        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/ifReadAll")
    public AjaxResult ifReadAll(@RequestParam("ids") Integer[] ids){
        AjaxResult ajaxResult = new AjaxResult();
        try{
            for (int i:ids) {
                messageService.setRead(i);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            e.printStackTrace();
        }

        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/deleteById")
    public AjaxResult deleteById(@RequestParam("id") Integer id){
        AjaxResult ajaxResult = new AjaxResult();
        try{
           messageService.deleteById(id);
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            e.printStackTrace();
        }

        return ajaxResult;
    }
}
